package cn.shop.dys.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.shop.lib_common.CommonUtil;
import cn.shop.dys.R;

/**
 * 增加删除et控件
 * Created by jian on 2016/4/14.
 */
public class ButtonEditText extends EditText {
    private String mButtonText;
    private Button mButton;
    private float mHeightPadding;
    private float mWidthPadding;
    private OnButtonClickListener mButtonListener;
    private Paint mPaint;
    private int mButtonWidth;
    private int mButtonHeight;
    private int mWidth;
    private int mHeight;

    public ButtonEditText(Context context) {
        super(context);
        init(null);
    }

    public ButtonEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public ButtonEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      init(attrs);
    }

    public void setOnButtonClickListener(OnButtonClickListener listener) {
        this.mButtonListener = listener;
    }

    public interface OnButtonClickListener {
        void onButtonClick(View view);
    }

    private void init(AttributeSet attrs) {
        if(attrs!=null){
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ButtonEditText);
            String buttonText = typedArray.getString(R.styleable.ButtonEditText_edit_text_button_text);
            if (!TextUtils.isEmpty(buttonText)) {
                this.mButtonText = buttonText;
            }
            typedArray.recycle();
        }
        mHeightPadding= CommonUtil.dpToPx(getContext(),17);
        mWidthPadding= CommonUtil.dpToPx(getContext(),17);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Rect buttonRect = new Rect();
        Log.d("test", mButtonText);
        mPaint.setTextSize(28);
        mPaint.setColor(Color.WHITE);
        mPaint.getTextBounds(mButtonText, 0, mButtonText.length(), buttonRect);
        mButtonWidth = buttonRect.width();
        mButtonHeight = buttonRect.height();
          Log.d("test", "mButtonWidth" + mButtonWidth + "mButtonHeight" + mButtonHeight);
        mButton = new Button(getContext());
        mButton.setBackgroundColor(Color.GREEN);
        Log.d("test", mButton + "bb");
        //   mButton.measure(0,0);
        mButton.setTextSize(12);
        mButton.setPadding(0,0,0,0);
        mButton.setText(mButtonText);
        mButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mButtonListener != null) {
                    mButtonListener.onButtonClick(v);
                }
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mButton.measure(MeasureSpec.makeMeasureSpec(mButtonWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(mButtonHeight, MeasureSpec.EXACTLY));
        Log.d("test", "测量了");
        //MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED)
        // mButton.measure(MeasureSpec,MeasureSpec.makeMeasureSpec(getMeasuredHeight()-2*mHeightPadding,MeasureSpec.EXACTLY));
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        float y = event.getY();
        float x = event.getX();
        if (y > mHeightPadding && y < mHeight - mHeightPadding && x > mWidth - mButtonWidth - mWidthPadding
                && x < mWidth - mWidthPadding) {
            return mButton.dispatchTouchEvent(event);
        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getWidth();
        mHeight = getHeight();
    }

   @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
     //   Log.d("test", "mWidth" + mWidth);
     //   Log.d("test", mButtonWidth + "mButtonWidth");
      // Log.d("test", mButtonHeight + "mButtonHeight");
       mButton.layout(0,0,mButtonWidth,mButtonHeight);
       /* mButton.layout(mWidth - mButtonWidth - mWidthPadding, mButtonHeight,
                mWidth - mWidthPadding, mButtonHeight + mButtonHeight);*/
       Log.d("test","布局了");
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mWidth - mButtonWidth - mWidthPadding, mHeightPadding);
        mButton.draw(canvas);
        canvas.restore();
        Log.d("test", "画画了");
    }
}
